package com.joyxy.easy.service;

import cn.hutool.log.Log;
import cn.sanenen.dm.base.DmApi;
import cn.sanenen.dm.common.constant.KeyConstants;
import cn.sanenen.dm.common.constant.MapConstant;
import com.joyxy.easy.entity.WindowContext;
import com.joyxy.easy.monitor.StatusMonitor;
import com.joyxy.easy.manger.WindowManager;

public class SpiderService {
    private static final Log log = Log.get();
    private DmApi instance;
    private MoveService moveService;
    StatusMonitor monitor;
    private long hwnd;

    public SpiderService(long hwnd) {
        this.hwnd = hwnd;
        instance = WindowManager.getContext(hwnd).getDmApi();
        monitor = WindowManager.getContext(hwnd).getMonitor();
        moveService = new MoveService(hwnd);
    }

    public void autoSell(int floor, String tdfKey,boolean sell) {
        int zzfloor = getFloor();
        if (zzfloor != -1) {
            floor = zzfloor;
        }

        log.info(monitor.getHwnd() + "获取蜘蛛层数：" + floor);
        WindowContext context = WindowManager.getContext(hwnd);
        context.getMonitor().setSell(true);
        try {
            instance.KeyPressChar(tdfKey);
            Thread.sleep(1000);
            long map = monitor.getMap();
            while (map != MapConstant.WAN_ZI_SHAN) {
                moveToWanziShan();
                Thread.sleep(500);
                map = monitor.getMap();
            }
            Thread.sleep(1000);
            if(sell){
                //角色移动到道具货郎附近
                moveToHuoLangAndClick();
                //卖装备
                sellZhuangbei();
                //等两分钟
                Thread.sleep(1000 * 60 * 2);
//            关掉道具货郎
                closeSell();
                //角色移动到道具货郎附近
                moveToHuoLangAndClick();
                Thread.sleep(1000);
                //卖葫芦
                sellHulu();
                //等一分钟
                Thread.sleep(1000 * 60 * 1);
                //关掉道具货郎
                closeSell();
            }
            //回到蜘蛛
            returnToZZ();
            while (monitor.getMap() != MapConstant.ZHI_ZHU) {
                log.info("没有进入蜘蛛，重试重");
                instance.KeyDown(KeyConstants.RIGHT);
                Thread.sleep(500);
                instance.KeyUp(KeyConstants.RIGHT);
                returnToZZ();
            }
            //进入蜘蛛
            moveService.move(526, null);
            instance.KeyPress(KeyConstants.SPACE);
            Thread.sleep(500);
//            moveService.move(739);
//            Thread.sleep(500);
            moveService.move(734, null);
            instance.KeyPress(KeyConstants.UP);
            Thread.sleep(500);
            upFloor(floor);
        } catch (Exception e) {
            log.error(e);
        } finally {
            context.getMonitor().setSell(false);
        }

    }

    private void sellHulu() throws InterruptedException {
        Thread.sleep(500);
        instance.MoveTo(346, 294);
        instance.LeftClick();
        Thread.sleep(500);
        instance.LeftClick();
        Thread.sleep(500);
    }

    private int getFloor() {
        WindowContext context = WindowManager.getContext(hwnd);
        StatusMonitor monitor = context.getMonitor();
        long map = monitor.getMap();
        if(MapConstant.ZHI_ZHU_6  == map) {
            int y = (int) monitor.getY();
            if (y > 0 && y < 300) {
                return 4;
            }
            if (y >= 300 && y < 500) {
                return 3;
            }
            if (y >= 500 && y < 650) {
                return 2;
            }
            if (y >= 650 && y < 950) {
                return 1;
            }
            return -1;
        }
        return -1;
    }

    private void moveToWanziShan() throws InterruptedException {
        Thread.sleep(1000);
        //鼠标移动到土地npc
        instance.MoveTo(310, 274);
        Thread.sleep(500);
        instance.RightClick();
        //移动到丸子山
        instance.MoveTo(378, 312);
        Thread.sleep(500);
        instance.LeftClick();
        Thread.sleep(500);
        //确定
        instance.MoveTo(413, 320);
        Thread.sleep(500);
        instance.LeftClick();
    }

    private void moveToHuoLangAndClick() throws InterruptedException {
        moveService.move(1106, null);//地图坐标x ->110
        instance.MoveTo(185, 263);
        Thread.sleep(500);
        instance.RightClick();
        Thread.sleep(500);
    }

    private void sellZhuangbei() throws InterruptedException {
        Thread.sleep(500);
        //卖掉所有装备
        instance.MoveTo(376, 354);
        Thread.sleep(500);
        instance.LeftClick();
        //移动到确定图标
        instance.MoveTo(413, 323);
        Thread.sleep(500);
        instance.LeftClick();
        Thread.sleep(500);
    }

    private void returnToZZ() throws InterruptedException {
        //走回蜘蛛
        moveService.move(535, null); //地图坐标x 53
        Thread.sleep(500);
        instance.MoveTo(55, 336);
        Thread.sleep(500);
        instance.RightClick();
        Thread.sleep(500);
        instance.MoveTo(385, 264);
        instance.LeftClick();
        Thread.sleep(500);
        instance.MoveTo(435, 450);
        instance.LeftClick();
        Thread.sleep(500);
        instance.MoveTo(56, 333);
        instance.RightClick();
        instance.MoveTo(385, 264);
        instance.LeftClick();
        Thread.sleep(500);
        instance.MoveTo(435, 450);
        instance.LeftClick();
        Thread.sleep(500);
        instance.LeftClick();
        Thread.sleep(500);
    }

    private void closeSell() throws InterruptedException {
        instance.MoveTo(481, 110);
        Thread.sleep(500);
        instance.LeftClick();
        Thread.sleep(500);
    }

    private void upFloor(int floor) throws InterruptedException {
        moveService.move(1476, null);
        instance.KeyPress(KeyConstants.UP);
        Thread.sleep(500);
        if (floor == 1) {
            moveService.move(553, null);
            return;
        }
        Thread.sleep(500);
        moveService.move(235, null);
        Thread.sleep(500);
        instance.KeyPress(KeyConstants.UP);
        Thread.sleep(500);
        if (floor == 2) {
            moveService.move(1077, null);
            return;
        }
        Thread.sleep(500);
        moveService.move(1325, null);
        Thread.sleep(500);
        instance.KeyPress(KeyConstants.UP);
        if (floor == 3) {
            moveService.move(593, null);
            return;
        }
        Thread.sleep(500);
        moveService.move(254, null);
        Thread.sleep(500);
        instance.KeyPress(KeyConstants.UP);
        Thread.sleep(500);
        if (floor == 4) {
            moveService.move(1039, null);
        }
    }
}
